import 'dart:isolate';

main() async {
  print('main start');

//  Isolate.spawn(calc, 100);

  // 1.创建管道
  ReceivePort receivePort = ReceivePort();
  // 2.创建新的Isolate
  Isolate isolate = await Isolate.spawn<SendPort>(foo, receivePort.sendPort);
  // 3.监听管道消息
  receivePort.listen((message) {
    print(message);
    receivePort.close();
    isolate?.kill(priority: Isolate.immediate);
  });

  print('main end');
}

void calc(int index) {
  var total = 0;
  for (var i = 0; i < 100; i++) {
    total += i;
  }
  print(total);
}

void foo(SendPort sendPort) {
  sendPort.send("Hello World");
}
